<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="工艺路线编号" prop="routeCode">
        <el-input
          v-model="queryParams.routeCode"
          placeholder="请输入工艺路线编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺路线名称" prop="routeName">
        <el-input
          v-model="queryParams.routeName"
          placeholder="请输入工艺路线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enableFlag">
        <el-select
          v-model="queryParams.enableFlag"
          placeholder="请选择是否启用"
          clearable
        >
          <el-option
            v-for="dict in dict.type.enable_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyidata:route:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyidata:route:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyidata:route:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyidata:route:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="routeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工艺路线编号" align="center" prop="routeCode">
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">{{
            scope.row.routeCode
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工艺路线名称" align="center" prop="routeName" />
      <el-table-column label="工艺路线说明" align="center" prop="routeDesc" />
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.enable_flag"
            :value="scope.row.enableFlag"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyidata:route:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyidata:route:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工艺路线名称" prop="routeName">
          <el-input v-model="form.routeName" placeholder="请输入工艺路线名称" />
        </el-form-item>
        <el-form-item label="工艺路线说明" prop="routeDesc">
          <el-input
            v-model="form.routeDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="是否启用" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio
              v-for="dict in dict.type.enable_flag"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-divider content-position="center">工艺组成信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddProRouteProcess"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteProRouteProcess"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="proRouteProcessList"
          :row-class-name="rowProRouteProcessIndex"
          @selection-change="handleProRouteProcessSelectionChange"
          ref="proRouteProcess"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="工序编码" prop="processCode" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.processCode"
                placeholder="请选择工序"
                filterable
                clearable
                @change="handleProcessSelect(scope.row)"
              >
                <el-option
                  v-for="item in processOptions"
                  :key="item.processCode"
                  :label="item.processCode"
                  :value="item.processCode"
                />
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="工序名称" prop="processName" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.processName"
                placeholder="请选择工序"
                filterable
                clearable
                @change="handleProcessSelectByName(scope.row)"
              >
                <el-option
                  v-for="item in processOptions"
                  :key="item.processName"
                  :label="item.processName"
                  :value="item.processName"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="序号" prop="orderNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNum" placeholder="请输入序号" />
            </template>
          </el-table-column>
          <el-table-column
            label="下一道工序"
            prop="nextProcessName"
            width="150"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.nextProcessName"
                placeholder="请输入下一道工序"
              />
            </template>
          </el-table-column>
          <el-table-column label="与下一道工序关系" prop="linkType" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.linkType"
                placeholder="请选择与下一道工序关系"
              >
                <el-option
                  v-for="dict in dict.type.link_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="准备时间" prop="defaultPreTime" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.defaultPreTime"
                placeholder="请输入准备时间"
              />
            </template>
          </el-table-column>
          <el-table-column label="等待时间" prop="defaultSufTime" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.defaultSufTime"
                placeholder="请输入等待时间"
              />
            </template>
          </el-table-column>
          <el-table-column label="甘特图显示颜色" prop="colorCode" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.colorCode"
                placeholder="请输入甘特图显示颜色"
              />
            </template>
          </el-table-column>
          <el-table-column label="关键工序" prop="keyFlag" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.keyFlag"
                placeholder="请输入关键工序"
              />
            </template>
          </el-table-column>
          <el-table-column label="质检确认" prop="isCheck" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.isCheck"
                placeholder="请输入质检确认"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-divider content-position="center">关联产品信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddProRouteProduct"
              >添加产品</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteProRouteProduct"
              >删除产品</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="proRouteProductList"
          :row-class-name="rowProRouteProductIndex"
          @selection-change="handleProRouteProductSelectionChange"
          ref="proRouteProduct"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="产品物料编码" prop="itemCode" width="200">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.itemCode"
                placeholder="请选择物料"
                filterable
                clearable
                @change="handleItemSelect(scope.row)"
              >
                <el-option
                  v-for="item in itemOptions"
                  :key="item.itemCode"
                  :label="item.itemCode"
                  :value="item.itemCode"
                />
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="产品物料名称" prop="itemName" width="200">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.itemName"
                placeholder="请选择物料"
                filterable
                clearable
                @change="handleItemSelectByName(scope.row)"
              >
                <el-option
                  v-for="item in itemOptions"
                  :key="item.itemName"
                  :label="item.itemName"
                  :value="item.itemName"
                />
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="规格型号" prop="specification" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.specification" readonly />
            </template>
          </el-table-column>

          <el-table-column label="单位" prop="unitOfMeasure" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.unitOfMeasure"
                placeholder="请选择单位"
                filterable
                clearable
              >
                <el-option
                  v-for="item in measureOptions"
                  :key="item.measureCode"
                  :label="item.measureName"
                  :value="item.measureCode"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="生产数量" prop="quantity" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.quantity"
                placeholder="请输入生产数量"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看工艺流程 -->
    <el-dialog
      title="查看工艺流程"
      :visible.sync="show"
      width="900px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工艺路线编号" prop="routeCode">
          <el-input
            v-model="form.routeCode"
            placeholder="请输入工艺路线编号"
            readonly
          />
        </el-form-item>
        <el-form-item label="工艺路线名称" prop="routeName">
          <el-input
            v-model="form.routeName"
            placeholder="请输入工艺路线名称"
            readonly
          />
        </el-form-item>
        <el-form-item label="工艺路线说明" prop="routeDesc">
          <el-input
            v-model="form.routeDesc"
            type="textarea"
            placeholder="请输入内容"
            readonly
          />
        </el-form-item>
        <el-form-item label="是否启用" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio
              v-for="dict in dict.type.enable_flag"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
            readonly
          />
        </el-form-item>
        <el-divider content-position="center">工艺组成信息</el-divider>
        <el-table
          :data="proRouteProcessList"
          :row-class-name="rowProRouteProcessIndex"
          @selection-change="handleProRouteProcessSelectionChange"
          ref="proRouteProcess"
        >
          <el-table-column label="序号" prop="orderNum" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.orderNum"
                placeholder="请输入序号"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="工序编码" prop="processCode" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.processCode"
                placeholder="请输入工序编码"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="工序名称" prop="processName" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.processName"
                placeholder="请输入工序名称"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column
            label="下一道工序"
            prop="nextProcessName"
            width="150"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.nextProcessName"
                placeholder="请输入下一道工序"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="与下一道工序关系" prop="linkType" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.linkType"
                placeholder="请选择与下一道工序关系"
                disabled
              >
                <el-option
                  v-for="dict in dict.type.link_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="准备时间" prop="defaultPreTime" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.defaultPreTime"
                placeholder="请输入准备时间"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="等待时间" prop="defaultSufTime" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.defaultSufTime"
                placeholder="请输入等待时间"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="甘特图显示颜色" prop="colorCode" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.colorCode"
                placeholder="请输入甘特图显示颜色"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="关键工序" prop="keyFlag" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.keyFlag"
                placeholder="请输入关键工序"
                readonly
              />
            </template>
          </el-table-column>
          <el-table-column label="质检确认" prop="isCheck" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.isCheck"
                placeholder="请输入质检确认"
                readonly
              />
            </template>
          </el-table-column>
        </el-table>
        <el-divider content-position="center">关联产品信息</el-divider>
        <el-table :data="proRouteProductList" border>
          <el-table-column label="产品物料编码" prop="itemCode" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemCode" readonly />
            </template>
          </el-table-column>
          <el-table-column label="产品物料名称" prop="itemName" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemName" readonly />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" prop="specification" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.specification" readonly />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unitOfMeasure" width="150">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.unitOfMeasure"
                placeholder="请选择单位"
                filterable
                clearable
                disabled
              >
                <el-option
                  v-for="item in measureOptions"
                  :key="item.measureCode"
                  :label="item.measureName"
                  :value="item.measureCode"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="生产数量" prop="quantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.quantity" readonly />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoute,
  getRoute,
  delRoute,
  addRoute,
  updateRoute,
} from "@/api/ruoyidata/route";

// 新增引入工序接口
import { listProcess } from "@/api/ruoyidata/process";

// 新增引入物料接口
import { listItem } from "@/api/ruoyidata/item";

// 新增引入单位接口
import { listMeasure } from "@/api/ruoyidata/measure";
export default {
  name: "Route",
  dicts: ["link_type", "enable_flag"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProRouteProcess: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺路线表格数据
      routeList: [],
      // 工艺组成表格数据
      proRouteProcessList: [],
      // 产品关联选中数据
      checkedProRouteProduct: [],
      // 产品关联表格数据
      proRouteProductList: [],
      // 新增工序选项数据
      processOptions: [],
      // 新增物料选项数据
      itemOptions: [],
      // 新增单位选项数据
      measureOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      show: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        routeCode: null,
        routeName: null,
        enableFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeName: [
          { required: true, message: "工艺路线名称不能为空", trigger: "blur" },
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getProcessList();
    this.getItemList();
    this.getMeasureList();
  },
  methods: {
    /** 获取单位列表 */
    getMeasureList() {
      listMeasure().then((response) => {
        this.measureOptions = response.rows;
      });
    },
    /** 获取工序列表 */
    getProcessList() {
      listProcess({ pageSize: 9999 }).then((response) => {
        this.processOptions = response.rows;
      });
    },
    // 新增：工序选择处理
    handleProcessSelect(row) {
      const selected = this.processOptions.find(
        (p) => p.processCode === row.processCode
      );
      if (selected) {
        row.processName = selected.processName;
      }
    },
    // 新增：通过名称选择处理
    handleProcessSelectByName(row) {
      const selected = this.processOptions.find(
        (p) => p.processName === row.processName
      );
      if (selected) {
        row.processCode = selected.processCode;
      }
    },
    /** 查询工艺路线列表 */
    getList() {
      this.loading = true;
      listRoute(this.queryParams).then((response) => {
        this.routeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.show = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        routeId: null,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.proRouteProcessList = [];
      this.proRouteProductList = []; // 新增
      this.resetForm("form");
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      this.form = {
        routeId: row.routeId,
        routeCode: row.routeCode,
        routeName: row.routeName,
        routeDesc: row.routeDesc,
        enableFlag: row.enableFlag,
        remark: row.remark,
      };
      const routeId = row.routeId;
      getRoute(routeId).then((response) => {
        // 调用接口获取完整数据
        this.form = response.data;
        this.proRouteProcessList = response.data.proRouteProcessList; // 绑定子表数据
        this.proRouteProductList = response.data.proRouteProductList; // 新增
        this.show = true;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.routeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺路线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const routeId = row.routeId || this.ids;
      getRoute(routeId).then((response) => {
        this.form = response.data;
        this.proRouteProcessList = response.data.proRouteProcessList;
        this.proRouteProductList = response.data.proRouteProductList; // 新增
        this.open = true;
        this.title = "修改工艺路线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.proRouteProcessList = this.proRouteProcessList;
          this.form.proRouteProductList = this.proRouteProductList; // 新增
          if (this.form.routeId != null) {
            updateRoute(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoute(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const routeIds = row.routeId || this.ids;
      this.$modal
        .confirm('是否确认删除工艺路线编号为"' + routeIds + '"的数据项？')
        .then(function () {
          return delRoute(routeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 工艺组成序号 */
    rowProRouteProcessIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 工艺组成添加按钮操作 */
    handleAddProRouteProcess() {
      let obj = {};
      obj.processCode = "";
      obj.processName = "";
      obj.orderNum = "";
      obj.nextProcessName = "";
      obj.linkType = "";
      obj.defaultPreTime = "";
      obj.defaultSufTime = "";
      obj.colorCode = "";
      obj.keyFlag = "";
      obj.isCheck = "";
      this.proRouteProcessList.push(obj);
    },
    /** 工艺组成删除按钮操作 */
    handleDeleteProRouteProcess() {
      if (this.checkedProRouteProcess.length == 0) {
        this.$modal.msgError("请先选择要删除的工艺组成数据");
      } else {
        const proRouteProcessList = this.proRouteProcessList;
        const checkedProRouteProcess = this.checkedProRouteProcess;
        this.proRouteProcessList = proRouteProcessList.filter(function (item) {
          return checkedProRouteProcess.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleProRouteProcessSelectionChange(selection) {
      this.checkedProRouteProcess = selection.map((item) => item.index);
    },
    // 添加产品行
    handleAddProRouteProduct() {
      let obj = {
        itemCode: "",
        itemName: "",
        specification: "",
        unitOfMeasure: "",
        quantity: "",
      };
      this.proRouteProductList.push(obj);
    },

    // 删除产品行
    handleDeleteProRouteProduct() {
      if (this.checkedProRouteProduct.length === 0) {
        this.$modal.msgError("请先选择要删除的产品数据");
      } else {
        this.proRouteProductList = this.proRouteProductList.filter(
          (item, index) => !this.checkedProRouteProduct.includes(index + 1)
        );
      }
    },

    // 产品复选框选中
    handleProRouteProductSelectionChange(selection) {
      this.checkedProRouteProduct = selection.map((item) => item.index);
    },

    // 产品行序号生成
    rowProRouteProductIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 获取物料列表 */
    getItemList() {
      listItem({ pageSize: 9999 }).then((response) => {
        this.itemOptions = response.rows;
      });
    },

    // 新增：物料选择处理
    handleItemSelect(row) {
      const selected = this.itemOptions.find(
        (p) => p.itemCode === row.itemCode
      );
      if (selected) {
        row.itemName = selected.itemName;
        row.specification = selected.specification;
        row.unitOfMeasure = selected.unitOfMeasure; // 自动带出单位
      }
    },
    // 新增：通过名称选择处理
    handleItemSelectByName(row) {
      const selected = this.itemOptions.find(
        (p) => p.itemName === row.itemName
      );
      if (selected) {
        row.itemCode = selected.itemCode;
        row.specification = selected.specification;
        row.unitOfMeasure = selected.unitOfMeasure;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ruoyidata/route/export",
        {
          ...this.queryParams,
        },
        `route_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.el-select {
  width: 100%;
}
</style>